package com.jiaai.cloud.gift.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.base.page.FrontPage;
import com.jiaai.cloud.base.util.RestResultGenerator;
import com.jiaai.cloud.gift.entity.TLiveGiftType;
import com.jiaai.cloud.gift.service.ITLiveGiftTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 佳爱网络科技
 * @since 2017-08-09
 */
@Controller
@RequestMapping("/api/manage/tLiveGiftType")
public class TLiveGiftTypeController {
	@Autowired
    private ITLiveGiftTypeService itLiveGiftTypeService;

    //查找所有的
    @RequestMapping(value = "/findByAll", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findByAll(FrontPage page) throws ServiceException {
        try {
            EntityWrapper wrapper=new EntityWrapper();
            return RestResultGenerator.genSuccessResult( itLiveGiftTypeService.selectPage(page.getPagePlus(),wrapper));
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }

    //    删除
    @RequestMapping(value = "/deleteId/{typeId}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public Object deleteId(@PathVariable("typeId") Integer typeId) throws ServiceException {
        try {
            itLiveGiftTypeService.deleteById(typeId);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }

    //改(有就改，没有就插入)
    @RequestMapping(value="/edit",method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object edit(@RequestBody TLiveGiftType tLiveGiftType)throws ServiceException{
        try {
            tLiveGiftType.setCreateTime(new Date());
            itLiveGiftTypeService.insertOrUpdate(tLiveGiftType);
            return null;
        } catch (Exception e) {
            throw new  ServiceException("编辑失敗",e.getMessage());
        }
    }

}
